Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

How to select a BLOB

Oracle Database Tips by Donald BurlesonApril 22, 2015 - Updated 12 October 1014

 

Question:  I want to read a BLOB into my PL/SQL but I don't understand how to modify the select statement to accept a large result value.  Is there a general stored procedure for reading a BLOB in Oracle?

Answer:  Oracle supports over 20 data types, and you can even define your own custom data types.  For large objects, Oracle offers the BLOB, CLOB and LONG RAW (obsolete).

There are several documented methods to unload blob from the database, mostly using PL/SQL with utl_file.put_raw (see MOSC note 330146.1) and with java with FileOutputStream (see MOSC note 247546.1) .

The following SQL*Plus script will run on UNIX/Linux and select a blob, using xxd to translate the blob output.  Remember, the blob could be a photograph, an audio file or a video:

sqlplus -s scott/tiger <<EOF |xxd -p -r > doc.pdf
set pages 0 lin 17000 long 1000000000 longc 16384
select document from emp where ename=user;
EOF

Note:  When defining a BLOB column, the LOB storage clause is not needed if the maximum size of the BLOB doesn't exceed 4,000 bytes. Up to 4,000 bytes can be stored in-line with the other data in the tablespace. If the length of the BLOB exceeds 4000 bytes it must be stored in either a system defaulted storage (the same as the default for the table it resides in) or in an explicitly defined LOB storage area.

Here are the details for defining, reading and writing BLOB columns:

Dr. Hall demonstrates the procedure for reading BLOB data types in his book "Oracle PL/SQL Tuning: Expert Secrets for High Performance Programming".  Here is a sample from Dr. Hall, reading a BLOB with PL/SQL:

CREATE OR REPLACE PROCEDURE extract_file(product_id in number) IS

vblob BLOB;
vstart NUMBER := 1;
bytelen NUMBER := 32000;
len NUMBER;
my_vr RAW(32000);
x NUMBER;

l_output utl_file.file_type;

BEGIN

-- define output directory
l_output := utl_file.fopen('DIR_TEMP', 'filename','wb', 32760);

vstart := 1;
bytelen := 32000;

-- get length of blob
SELECT dbms_lob.getlength(productblob)
INTO len
FROM products
WHERE id = product_id;

-- save blob length
x := len;

-- select blob into variable
SELECT product_blob
INTO vblob
FROM products
WHERE id = product_id;

-- if small enough for a single write
IF len < 32760 THEN
utl_file.put_raw(l_output,vblob);
utl_file.fflush(l_output);
ELSE -- write in pieces
vstart := 1;
WHILE vstart < len and bytelen > 0
LOOP
   dbms_lob.read(vblob,bytelen,vstart,my_vr);

   utl_file.put_raw(l_output,my_vr);
   utl_file.fflush(l_output);

   -- set the start position for the next cut
   vstart := vstart + bytelen;

   -- set the end position if less than 32000 bytes
   x := x - bytelen;
   IF x < 32000 THEN
      bytelen := x;
   END IF;
END IF;
utl_file.fclose(l_output);
end loop;

Also see these notes on reading and writing BLOBs:

If you like Oracle tuning, you might enjoy my book "Oracle Tuning: The Definitive Reference", with 950 pages of tuning tips and scripts. 

You can buy it direct from the publisher for 30%-off and get instant access to the code depot of Oracle tuning scripts.


 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.